﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DTO;
using BUS;

namespace TCV_WEB.Admin.Page
{
    public partial class OrderUser : System.Web.UI.Page
    {

        private int iNewcatid = -1;
        private String sErr = "";
        private String _sErr = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (Session["userid"] != null)
                {
                    LoadDatatoFrom(Convert.ToInt32(Session["userid"]));
                }
            }
        }

        protected void LoadDatatoFrom(int iduser)
        {
            BUS.UserControl userctr = new BUS.UserControl();
            UserInfo userinfo = new UserInfo();
            userinfo = userctr.Get(iduser, ref sErr);

            //txtfullname.Text = userinfo.FullName;
            txtemail.Text = userinfo.Email;
            txtpassword.Text = userinfo.Password;
            txtmobi.Text = userinfo.Mobile;
            //txtaddress.Text = userinfo.Address;
            txtcity.Text = userinfo.City;
            txtcountry.Text = userinfo.Country;
            rblIsViewed.SelectedValue = userinfo.Sex.ToString();

        }
        protected UserInfo ReadFormtoUser()
        {
            UserInfo userinfo = new UserInfo();

            userinfo.ID = Convert.ToInt32(Session["userid"]);
            //userinfo.FullName = txtfullname.Text;
            userinfo.Email = txtemail.Text;
            userinfo.Password = txtpassword.Text;
            userinfo.Mobile = txtmobi.Text;
            //userinfo.Address = txtaddress.Text;
            userinfo.City = txtcity.Text;
            userinfo.Country = txtcountry.Text;
            //userinfo.Sex =Convert.ToBoolean(rblIsViewed.SelectedValue);

            return userinfo;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            UserInfo userinfo = ReadFormtoUser();
            BUS.UserControl userctr = new BUS.UserControl();
            userctr.Update(userinfo);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/Page/User.aspx");
            Session["userid"] = null;
        }

        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "USERID")
            {
                try
                {
                    int inewsid = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString().Trim());
                    OrderControl prd_control = new OrderControl();
                    DTO.OrderInfo odInfo = prd_control.Get(inewsid, ref sErr);
                    Session["userid"] = odInfo.UserID;
                    Response.Redirect("~/Admin/Page/User.aspx");
                    
                }
                catch (System.Exception ex)
                {
                    lblErr.Text = ex.Message;
                }
            }
            else if (e.CommandName.ToUpper() == "LOAD")
            {
                int inewsid = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString().Trim());

                Session["orderid"] = inewsid;
                Response.Redirect("~/Admin/Page/DetailOrder.aspx");
                Session["userid"] = null;
            }
        }

        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            LoadDataGrid();
        }

        private void LoadDataGrid()
        {
            DataTable dtList = new DataTable();
            try
            {
                OrderControl prd_control = new OrderControl();
                dtList = prd_control.GetAll_OrderEx(ref _sErr);

                RadGrid1.DataSource = dtList;
            }
            catch (Exception ex)
            {
                lblErr.Text = ex.Message;
            }
        }

    }
}
